package com.olddriver.crm.web.controller;

import com.olddriver.crm.domain.Employee;
import com.olddriver.crm.domain.PaidList;
import com.olddriver.crm.page.PageResult;
import com.olddriver.crm.query.PaymentQueryObject;
import com.olddriver.crm.service.IInsuranceListService;
import com.olddriver.crm.service.IPaidListService;
import com.olddriver.crm.util.JsonResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * Created by A on 2017/12/4.
 */
@Controller
@RequestMapping("payment")
public class PaymentController {

    @Autowired
    private IPaidListService paidListService;

    @Autowired
    private IInsuranceListService insuranceList;

    @RequestMapping("list")
    public String payment() {

        return "payment/payment";
    }

    @ResponseBody
    @RequestMapping("query")
    public PageResult query(PaymentQueryObject qo) {
        return insuranceList.query(qo);
    }

    @ResponseBody
    @RequestMapping("pay")
    public JsonResult pay(Long Iid, PaidList paidList) {
        try {
            //更新状态为已收款
            paidListService.pay(Iid);

            //存入业务表单
            long time = new Date().getTime();
            paidList.setSn(time+"");
            Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
            paidList.setInputUser(employee);

            paidListService.insert(paidList,Iid);

            return JsonResult.diySuccess("缴费成功，业务单号："+time);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }

    @ResponseBody
    @RequestMapping("checkpassword")
    public JsonResult checkpassword(String password) {
        try {
            Employee principal = (Employee) SecurityUtils.getSubject().getPrincipal();
            String dataBasePassword = new Md5Hash(password, principal.getUsername(), 2).toString();
                if (!dataBasePassword.equals(principal.getPassword())) {
                    return JsonResult.diyFaliure("密码错误！想被扣工资吗？");
                }
            return JsonResult.diySuccess("密码正确");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }

}
